<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\video\model;

use phpDocumentor\Reflection\Type;

/**
 * Description of live
 * 直播
 * @author zmh
 */
class Video extends \web\common\model\BaseModel{
    
    protected $rows = 10;
    protected function _initialize(){
        $this->tableName = 'video';
    }

    /**
     * 后台列表
     * @param int $pageIndex
     * @param int $pageSize
     * @param string $filter
     * @param string $fields
     * @param string $order
     * @return mixed
     */
    public function getVideoList($pageIndex = -1, $pageSize = -1, $filter = '', $fields = 'a.*,b.title,c.video_title', $order = 'id desc') {
        $sql = 'select '.$fields.' from '.$this->getTableName() .' a left join tp_member_account b on b.id=a.user_id left join tp_video_class c on c.id=a.class_id';
        if (!empty($filter))
            $sql .= ' where ' . $filter;
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }

    /**
     * 上传列表
     * @param int $pageIndex
     * @param int $pageSize
     * @param string $filter
     * @param string $fields
     * @param string $order
     * @return mixed
     */
    public function getUploadVideoList($pageIndex = -1, $pageSize = -1, $filter = '', $fields = 'a.*,b.title,c.video_title', $order = 'id desc') {
        $sql = 'select '.$fields.' from '.$this->getTableName() .' a left join tp_member_account b on b.id=a.user_id ';
        if (!empty($filter))
            $sql .= ' where ' . $filter;
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }

    /**
     * 后台列表数量
     * @param string $filter
     * @return int
     */
    public function getVideoTotal($filter = '') {
        $sql = 'select count(*) as c from '.$this->getTableName() .' a left join tp_member_account b on b.id=a.user_id left join tp_video_class c on c.id=a.class_id';
        if (!empty($filter)){
            $sql = $sql . ' where ' . $filter;
        }
        $result = $this->query($sql);
        if (count($result) > 0)
            return intval($result[0]['c']);
        else
            return 0;
    }

    /**
     * 修改点赞和评论数量
     * @param $video_id 视频id
     * @param $type 1=点赞数量 2=评论数量
     * @param $status 1增加  2减少
     * @return bool
     */
    public function updatePraiseCom($video_id, $type, $status){
        if($status == 1){
            if($type == 1){
                $re = $this->where(['id' => $video_id])->setInc('praise_number', 1);
                if(!$re) return false;
            }else{
                $re = $this->where(['id' => $video_id])->setInc('com_number', 1);
                if(!$re) return false;
            }
        }else{
            if($type == 1){
                $re = $this->where(['id' => $video_id])->setDec('praise_number', 1);
                if(!$re) return false;
            }else{
                $re = $this->where(['id' => $video_id])->setDec('com_number', 1);
                if(!$re) return false;
            }
        }
        return true;
    }

    /**
     * 获取记录总数
     * @param type $filter
     * @return int
     */
    public function getTotal($filter = '') {
        $sql = 'select count(id) as c from '.$this->getTableName();
        if (!empty($filter)){
            $sql = $sql . ' where 1=1 ' . $filter;
        }
        $result = $this->query($sql);
        if (count($result) > 0)
            return intval($result[0]['c']);
        else
            return 0;
    }


    public function getDataList($pageIndex = -1, $pageSize = -1, $filter = '',$fileds='*', $order = 'id desc') {
        $sql = 'select '.$fileds.' from '.$this->getTableName() . ' where 1=1 '.$filter;
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }

    
}
